﻿/*
 * taskinfo.cpp
 *
 *  Created on: 2018年2月18日
 *      Author: work
 */

#include <dm/export.hpp>

#define DM_API_OS_SYS DM_API_EXPORT

#include <dm/os/sys/taskinfo.hpp>
#include <dm/os/scopelock.hpp>

namespace dm{
namespace os{
namespace sys{

static const char* modeStr[] = {
		"禁止执行",
		"守护执行",
		"单次执行",
		"手动执行",
		"每秒执行",
		"每分钟执行",
		"每小时执行",
		"每天执行",
		"每月执行",
		"每年执行",
		"每周一执行",
		"每周二执行",
		"每周三执行",
		"每周四执行",
		"每周五执行",
		"每周六执行",
		"每周日执行",

		"未定义类型"
};

void STaskInfo::init(){
	dm::os::CScopeLock slock(lock);
	disable = false;
	taskState = TsInited;
}

bool STaskInfo::ifExist(){
	dm::os::CScopeLock slock(lock);
	return ifProcess(pid);
}

bool STaskInfo::ifRunning(){
	dm::os::CScopeLock slock(lock);
	return ifProcess(pid) && (taskState==TsInited || taskState==TsRunning);
}

void STaskInfo::enable( bool e ){
	dm::os::CScopeLock slock(lock);
	disable = !e;
}

void STaskInfo::stop(){
	dm::os::CScopeLock slock(lock);
	taskState = TsExting;
}

const char* STaskInfo::startMode2str( const STaskInfo::EStartMode& mode ){
	if( mode<0 || mode>=SmUnknow )
		return modeStr[SmUnknow-1];
	else
		return modeStr[mode];
}

std::ostream& operator<<(std::ostream& ostr,const STaskInfo& info ){
	ostr  <<"描述:"<<info.desc.c_str()
			 <<" 启动方式:"<<info.startModeStr()
			<<" 下次执行时刻："<<dm::CDateTime(info.nextExecTs).toString()
			<<" 最后状态:"<<taskState2String(info.taskState)
			<<" 最后更新时刻:"<<dm::CDateTime(info.refreshTs).toString()
			<<" 最后检测时刻"<<dm::CDateTime(info.lastExamTs).toString()
			<<" 启动秒延时:"<<info.delaySecs
			<<" 启动时刻:"<<info.time.toString()
			<<" 进程号:"<<info.pid
			<<" 是否禁用:"<< (info.disable?"禁用":"允许")
			<<" 命令:"<<info.program.c_str()
			<<" 参数:"<<info.paras.c_str();

	return ostr;
}

}
}
}


